#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
        vector<int> ret;

        map<string, int> mp;
        for (const auto& e : words) mp[e]++;

        size_t len = words[0].size();
        if (s.size() < len * words.size())return ret;

        for (size_t start = 0; start < len; start++)
        {
            int count = 0, size = words.size();
            map<string, int> cur;
            for (size_t left = start, right = start; right < s.size(); right += len)
            {
                string tmp = s.substr(right, len);
                if (mp.count(tmp) && cur[tmp]++ < mp[tmp])count++;
                if (right - left + 1 > words.size() * len)
                {
                    string tmp2 = s.substr(left, len);
                    left += len;
                    if (mp.count(tmp2) && cur[tmp2]-- <= mp[tmp2])count--;
                }
                if (count == words.size())ret.push_back(left);
            }
        }
        return ret;
    }
};